package pl.praca.magisterska.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Validator;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import pl.praca.magisterska.model.Person;
import pl.praca.magisterska.service.PersonService;

@Controller
@SessionAttributes
public class LoginController {

	
	private static Logger log = Logger.getLogger(LoginController.class);
	
	@Autowired
	private PersonService service;
	
	@Autowired
	private Validator validator;
	
	public PersonService getService() {
		return service;
	}
	public void setService(PersonService service) {
		this.service = service;
	}

	public Validator getValidator() {
		return validator;
	}
	public void setValidator(Validator validator) {
		this.validator = validator;
	}
	
//	@RequestMapping(value = "/login", method = RequestMethod.POST)
//	public String addContact(@ModelAttribute("contact") Contact contact,
//			BindingResult result) {
		
//		log.info("post");
//		log.debug("First Name:" + contact.getFirstname() + "Last Name:"
//				+ contact.getLastname());
		
//		validator.validate(contact, result);  
//	    if (result.hasErrors()) { return "contactView"; } 
//		return "redirect:addContact.html";
//	}

      
	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public ModelAndView show(HttpServletRequest request) throws Exception {
		
		HttpSession session = request.getSession(true);
		Person person = new Person();
		String message;
		String userIdS = request.getParameter("userId");
		Integer userId;
		
		log.info("get");
	
		
		Object o = session.getAttribute("person");
		if (o!=null && o instanceof Person){
			log.info("Jestes ju� zalogowany");
			message = "Jestes ju� zalogowany";
			request.setAttribute("message", message);
			
			session.setAttribute("person", session.getAttribute("person"));
			return new ModelAndView("loginView");
		}else{
			if(userIdS == null || userIdS.isEmpty()){
				message = "Nie zosta� wprowadzony ID u�ytkownika!";
				request.setAttribute("message", message);
				session.setAttribute("person", session.getAttribute("person"));
				return new ModelAndView("loginView");
			}
			try{
				userId = Integer.parseInt(userIdS);
			}catch(Exception e) {
				message = "B��dny identyfikator u�ytkownika!";
				request.setAttribute("message", message);
				session.setAttribute("person", session.getAttribute("person"));
				return new ModelAndView("loginView");
			}
				
				log.info("userId: " + userId);
		
			if(userId == null || userId<=0){
				message = "B��dny identyfikator u�ytkownika!";
				request.setAttribute("message", message);
				session.setAttribute("person", session.getAttribute("person"));
				return new ModelAndView("loginView");
			}
			person = service.selectPerson(userId);			
			if(person == null){
				message = "W bazie nie istnieje u�ytkownik o ID = " + userId;
				request.setAttribute("message", message);
				session.setAttribute("person", null);
				return new ModelAndView("loginView");
			}else{

				session.setAttribute("person", person);
				
			}
				
			return new ModelAndView("mainView");
			
		}		
	}
}
